package com.caregiver.watch.business.data.parser;

import com.google.protobuf.InvalidProtocolBufferException;
import com.caregiver.watch.business.data.protobuf.PhoneBook;
import com.caregiver.watch.business.data.utils.time.DateTimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import static com.caregiver.watch.business.data.enums.PayloadOptTypeProtoEnum.OLD_ALARM;

/**
 * 旧版告警数据解析
 */
@Component
@Slf4j
public class OldAlarmDataParser implements IDataParser {

    @Override
    public Integer getOpt() {
        return OLD_ALARM.getOpt();
    }

    @Override
    public void dataParse(Long userId, String deviceId, byte[] payload) {
        PhoneBook.PhoneBookConfirm alarmPhonebook = null;
        try {
            alarmPhonebook = PhoneBook.PhoneBookConfirm.parseFrom(payload);
        } catch (InvalidProtocolBufferException ex) {
            ex.printStackTrace();
            return;
        }
        if (alarmPhonebook.hasAlarminfo()) {
            PhoneBook.PhoneAlarminfo alarmInfo = alarmPhonebook.getAlarminfo();
            if (alarmInfo.hasSleepstate()) {
                String alarmTime = DateTimeUtils.convert2DateTimeString(alarmInfo.getTimeStamp().getDateTime().getSeconds());
                log.info("sleep alarm time:{}", alarmTime);
            }
        }
    }
}
